package com.ye.zcswzlms.shiro;

import com.ye.zcswzlms.entity.Users;
import com.ye.zcswzlms.service.IUsersService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

public class CustomerRealm extends AuthorizingRealm {
    @Autowired
    private IUsersService usersService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        Users users = usersService.getUsers(authenticationToken.getPrincipal().toString());
        if (!ObjectUtils.isEmpty(users)) {
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(users.getStuNo(), users.getPassword(), ByteSource.Util.bytes(users.getPasswordSalt()),this.getName());
            return simpleAuthenticationInfo;
        }
        return null;
    }
}
